//
//  FDNextViewController.m
//  Fragrance
//
//  Created by yinzhongyuan on 15/5/25.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "FDNextViewController.h"

@interface FDNextViewController ()

@end

@implementation FDNextViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIImage *image = [UIImage imageNamed:@"btn_nav_share@2x"];
    image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(share)];
    
    
    [TAOverlay showOverlayWithLabel:nil Options:TAOverlayOptionOverlaySizeRoundedRect];
    
   

//提示的同时加载数据

    [self downLoadThisPage];





}

- (void)downLoadThisPage
{
    
    NSString *str = [NSString stringWithFormat:@"http://api.2meiwei.com/v1/recipe/%@/&appname=mw_android&appver=1.0.1&osver=4.4.2&devicename=H60-L01&openudid=866568021277461", self.ID];
    str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:str];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
       
        [TAOverlay hideOverlay];
        //数据请求完成，开始解析
        self.FDarrayData = [NSMutableArray array];
        self.FDarrayData = [FDNextJX getNextModel:data];
        
        
        self.FDStepArray = [NSMutableArray array];
        self.FDStepArray = [FDNextStepJX getStepModel:data];
        //添加tableview视图
        [self initTableView];
    }];
    
    
    
    
    
    
}





- (void)initTableView
{
    myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 50) style:UITableViewStylePlain];
    myTableView.delegate = self;
    myTableView.dataSource = self;
    myTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    [self.view addSubview:myTableView];
    [myTableView release];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   // model ＝ [array objectatindex: indexPath.row];
//    if (model 有url数据) {
//        ／／选择带有imageview的cell
//    }
    model = [self.FDarrayData objectAtIndex:0];
    
    if (indexPath.row == 0) {
        
            static NSString *identifer = @"cell";
            FDOneTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifer];
            if (cell == nil) {
                cell = [[FDOneTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifer];
            }
        
        [cell.BGimage setImageWithURL:[NSURL URLWithString:self.picIcon] placeholderImage:[UIImage imageNamed:@"jiazaitu"]];
        
            cell.titleLabel.text = model.title;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        return cell;
        
    }else if(indexPath.row == 1){
    
    
    static NSString *identifer = @"cell";
    FDTwoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifer];
    if (cell == nil) {
        cell = [[FDTwoTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifer];
    }
    
        cell.tasteLabelD.text = @"口味";
        cell.tasteLabel.text = model.taste;
        
        cell.timeLabelD.text = @"时间";
        cell.timeLabel.text = model.during;
        
        cell.nanduLabelD.text = @"难度";
        cell.nanduLabel.text = model.level;
        
        cell.artLabelD.text = @"工艺";
        cell.artLabel.text = model.zuofa;
        
        cell.step.text = @"提示及制作步骤";
        
        NSString *str = [NSString stringWithFormat:@"发表时间 %@", model.time];
        cell.showLabel.text = str;
        
        NSString *str1 = [NSString stringWithFormat:@"收藏数:%@  评论数:%@", model.saveNum, model.replyNum];
        cell.num.text = str1;
        
        UIImage *image = [UIImage imageNamed:@"beijing6"];
        UIImageView *imageview = [[UIImageView alloc] initWithImage:image];
        cell.backgroundView = imageview;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [imageview release];
        
        return cell;
    
    }
    else if (indexPath.row == 2){
        static NSString *identifer = @"cell1";
        FDFourTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifer];
        if (cell == nil) {
            cell = [[FDFourTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifer];
        }
        
        
        NSString *str = model.tips;
        NSString *str1 = [str stringByReplacingOccurrencesOfString:@"<br>" withString:@""];
        NSString *str2 = [str1 stringByReplacingOccurrencesOfString:@"<br />" withString:@""];
        NSString *str3 = [str2 stringByReplacingOccurrencesOfString:@"&nbsp;" withString:@""];
        cell.tipsLabel.text = str3;
        
        cell.tipsLabelD.text = @"温馨小提示";
        
        UIView *view = [[UIView alloc] init];
        view.backgroundColor = [UIColor colorWithRed:219 / 255.0 green:150 / 255.0 blue:250 / 255.0 alpha:1];
        cell.backgroundView = view;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [view release];
        return cell;
    }

    else if (indexPath.row >= 3){
        static NSString *identifer = @"cell2";
        FDThreeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifer];
        if (cell == nil) {
            cell = [[FDThreeTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifer];
        }
        
        model1 = [self.FDStepArray objectAtIndex:(indexPath.row - 3)];
        
        cell.stepLabel.text = model1.note;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;


        [cell.stepIcon setImageWithURL:[NSURL URLWithString:model1.stepIcon] placeholderImage:[UIImage imageNamed:@"jiazaitu"]];
        cell.stepIcon.contentMode = UIViewContentModeScaleAspectFill;
        cell.backgroundColor = [UIColor colorWithRed:219 / 255.0 green:135 / 255.0 blue:49 / 255.0 alpha:0.1];
        

        return cell;
        
    }
    
            return nil;

}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1 + 1 + 1 + self.FDStepArray.count;
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        
        return 280;
    }else if (indexPath.row == 1){
        return 180;
    }else if(indexPath.row == 2)
    {
        model = [self.FDarrayData objectAtIndex:0];
        CGFloat hight = [AutoHeight getLabelHight:13.0 :self.view.frame.size.width :model.tips];
        
        return 80 + hight - 50;
    }

    else {
        model1 = [self.FDStepArray objectAtIndex:(indexPath.row - 3)];
        CGFloat hight = [AutoHeight getLabelHight:13.0 :self.view.frame.size.width - 100:model1.note];
        return 135 +hight - 30;
    
    }
    
}




//实现分享功能
- (void)share

{
    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:self.picIcon]];
    UIImage *image = [UIImage imageWithData:data];
    [UMSocialSnsService presentSnsIconSheetView:self
                                         appKey:@"5567c23767e58eacd50014c3"
                                      shareText:model.title
                                     shareImage:image
                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToTencent, UMShareToRenren, nil]
                                       delegate:nil];
    
   
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
